﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using WoW_Downloader.Model;
using Download_Libary;
using WoW_Downloader.Properties;
using System.Threading;

namespace WoW_Downloader.Controller
{
    class DownloadController
    {
        #region Fields
        public DownloadModel ViewData;

        private TorrentDownload m_TbcTorrentDownload;

        private TorrentDownload m_WotlkTorrentDownload;

        private Settings m_Settings;

        private Timer m_Timer;

        private bool m_WotlkIsDownloading = false;

        private bool m_TbcIsDownloading = false;

        #endregion

        public DownloadController()
        {
            ViewData = new DownloadModel();
            ViewData.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ViewData_PropertyChanged);
            m_Settings = Settings.Default;
            pushConfigToModel();
        }

        void ViewData_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "PortNumber":
                    m_Settings.m_ListenerPort = ViewData.PortNumber;
                    break;
                case "SavePath":
                    m_Settings.m_SavePath = ViewData.SavePath.FullName;
                    break;
                case "GlobalUploadSpeed":
                    setUploadSpeed(ViewData.GlobalUploadSpeed);
                    break;
                case "GlobalDownloadSpeed":
                    setDownloadSpeed(ViewData.GlobalDownloadSpeed);
                    break;
            }
            m_Settings.Save();
        }

        private void pushConfigToModel()
        {
            ViewData.PortNumber = m_Settings.m_ListenerPort;
            ViewData.GlobalDownloadSpeed = m_Settings.m_MaxDownloadSpeed;
            ViewData.GlobalUploadSpeed = m_Settings.m_MaxUploadSpeed;

            if (Directory.Exists(m_Settings.m_SavePath))
                ViewData.SavePath = new DirectoryInfo(m_Settings.m_SavePath);
        }

        private void setUploadSpeed(int newUploadSpeed)
        {
            m_Settings.m_MaxUploadSpeed = newUploadSpeed;

            newUploadSpeed *= 1000;

            if (newUploadSpeed == 0)
                newUploadSpeed = 99999999;

            if (m_TbcTorrentDownload != null)
                m_TbcTorrentDownload.ChangeUploadSpeed(newUploadSpeed);

            if (m_WotlkTorrentDownload != null)
                m_WotlkTorrentDownload.ChangeUploadSpeed(newUploadSpeed);
        }

        private void setDownloadSpeed(int newDownloadSpeed)
        {
            m_Settings.m_MaxDownloadSpeed = newDownloadSpeed;

            newDownloadSpeed *= 1000;

            if (newDownloadSpeed == 0)
                newDownloadSpeed = 99999999;

            if (m_TbcTorrentDownload != null)
                m_TbcTorrentDownload.ChangeDownloadSpeed(newDownloadSpeed);

            if (m_WotlkTorrentDownload != null)
                m_WotlkTorrentDownload.ChangeDownloadSpeed(newDownloadSpeed);

        }

        public void chooseFolder()
        {
            using (System.Windows.Forms.FolderBrowserDialog objDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                objDialog.Description = "Bitte wählen sie den Torrent Speicherort aus";
                objDialog.SelectedPath = @"C:\";       // Vorgabe Pfad (und danach der gewählte Pfad)
                System.Windows.Forms.DialogResult objResult = objDialog.ShowDialog();

                if (objResult == System.Windows.Forms.DialogResult.OK)
                {
                    ViewData.SavePath = new DirectoryInfo(objDialog.SelectedPath);
                }
            }
        }

        public void DownloadTbc()
        {
            if (m_TbcIsDownloading == false && ViewData.SavePath != null && ViewData.PortNumber != 0)
            {
                m_Timer = new Timer((a) => { getTbcSpeed(); });

                m_TbcTorrentDownload = new TorrentDownload(ViewData.SavePath.FullName, ViewData.PortNumber);

                m_TbcTorrentDownload.DownloadTorrent(Resources.at_bc);

                m_Timer.Change(0, 1000);

                m_TbcIsDownloading = true;
            }
        }

        public void DownloadWotlk()
        {
            if (m_WotlkIsDownloading == false && ViewData.SavePath != null && ViewData.PortNumber != 0)
            {
                m_Timer = new Timer((a) => { getWotlkSpeed(); });

                m_WotlkTorrentDownload = new TorrentDownload(ViewData.SavePath.FullName, ViewData.PortNumber);

                m_WotlkTorrentDownload.DownloadTorrent(Resources.at_wotlk);

                m_Timer.Change(0, 1000);

                m_WotlkIsDownloading = true;
            }
        }

        internal void StopTbcDownload()
        {
            if (m_TbcIsDownloading == true)
            {
                m_TbcTorrentDownload.StopTorrent();
                m_TbcIsDownloading = false;
            }
        }

        internal void StopWotlkDownload()
        {
            if (m_WotlkIsDownloading == true)
            {
                m_WotlkTorrentDownload.StopTorrent();
                m_WotlkIsDownloading = false;
            }
        }

        private void getTbcSpeed()
        {
            int downloadSpeed = m_TbcTorrentDownload.TotalDownloadSpeed;
            int uploadSpeed = m_TbcTorrentDownload.TotalUploadSpeed;

            ViewData.TbcDownloadSpeed = parseSpeed(downloadSpeed);
            ViewData.TbcUploadSpeed = parseSpeed(uploadSpeed);

            ViewData.TbcProgress = m_TbcTorrentDownload.Progress;
        }

        private string parseSpeed(int speed)
        {
            string newSpeed = String.Empty;
            double doubleSpeed = 0.0;

            speed /= 1024;

            if (speed >= 1024)
            {
                doubleSpeed = speed / 1000;
                newSpeed = doubleSpeed + " " + "MB/s";
            }
            else if (speed < 1024)
            {
                doubleSpeed = speed;
                newSpeed = doubleSpeed + " "+ "KB/s";
            }

            return newSpeed;
        }

        private void getWotlkSpeed()
        {
            int downloadSpeed = m_WotlkTorrentDownload.TotalDownloadSpeed;
            int uploadSpeed = m_WotlkTorrentDownload.TotalUploadSpeed;

            ViewData.WotlkDownloadSpeed = parseSpeed(downloadSpeed);
            ViewData.WotlkUploadSpeed = parseSpeed(uploadSpeed);

            ViewData.WotlkProgress = m_WotlkTorrentDownload.Progress;
        }

        internal void CloseWindow()
        {
            if (m_TbcTorrentDownload != null)
                m_TbcTorrentDownload.Dispose();

            if (m_WotlkTorrentDownload != null)
                m_WotlkTorrentDownload.Dispose();
        }
    }
}